﻿using MVC_basic.Data_Layer;
using MVC_basic.Data_Layer.Interface;
using MVC_basic.Helpers;
using MVC_basic.Models;
using MVC_basic.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace MVC_basic.Controllers
{
    [Authorize]
    public class CreateAppController : Controller
    {
        private readonly IdbApp db;
        public CreateAppController(IdbApp DIdb)
        {
            db = DIdb;
            WebSecurity_Singleton.init();
        }

        public ActionResult Index()
        {
            CanvasAppViewModel appVM = new CanvasAppViewModel();
            return View(appVM);
        }

        [HttpPost]
        public ActionResult Caja(CajaAppViewModel appInfo, HttpPostedFileBase zipfile)
        {
            App app = null;
            int userId = WebSecurity.GetUserId(User.Identity.Name);
            if (ModelState.IsValid)
            {
                app = db.addCajaApp(appInfo.name, appInfo.logoUrl, appInfo.acceptedUrl, userId);
            }
            REST_Helper.send(app.id, zipfile);
            return RedirectToAction("Edit", "EditApp", new { id = app.id });
        }
       
        [HttpPost]
        public ActionResult Iframe(CanvasAppViewModel appInfo)
        {
            App app = null;
            if (ModelState.IsValid)
            {
                int userId = WebSecurity.GetUserId(User.Identity.Name);
                app = db.addCanvasApp(appInfo.name, appInfo.logoUrl, appInfo.url, userId, appInfo.canvasWidth, appInfo.canvasHeight);
            }
            return RedirectToAction("Edit", "EditApp", new { id = app.id });
        }

        public PartialViewResult CajaApp()
        {
            return PartialView();
        }
        public PartialViewResult CanvasApp()
        {
            return PartialView();
        }
    }
}
